﻿/***********************************************************************
 * <copyright file="NumberBoxExtension.cs" company="BUCA JSC">
 * -->    Copyright (C) statement. All right reserved
 * </copyright>
 * 
 * Created:   LinhMC
 * Email:    linhmc@buca.vn
 * Website:
 * Create Date: Friday, November 15, 2013
 * Usage: 
 * 
 * RevisionHistory: 
 * Date         Author               Description 
 * 
 * ************************************************************************/

using System.Globalization;
using System.Web.Mvc;

namespace EasyUIMvc
{
    public class NumberBoxExtension : ExtensionBase
    {
        private readonly NumberBoxSettings _settings;

        public NumberBoxExtension(NumberBoxSettings settings)
        {
            _settings = settings;
        }


        public override string GetScripts()
        {
            throw new System.NotImplementedException();
        }

        public override MvcHtmlString GetHtml()
        {
            if (!string.IsNullOrEmpty(_settings.NumberFormat.CurrencyDecimalDigits.ToString(CultureInfo.InvariantCulture)))
            {
                _settings.DataOptions = "precision:'" + _settings.NumberFormat.CurrencyDecimalDigits.ToString(CultureInfo.InvariantCulture) + "'";
            }
            if (!string.IsNullOrEmpty(_settings.NumberFormat.CurrencyGroupSeparator))
            {
                _settings.DataOptions += ",groupSeparator: '" + _settings.NumberFormat.CurrencyGroupSeparator + "'";
            }
            if (!string.IsNullOrEmpty(_settings.NumberFormat.CurrencyDecimalSeparator))
            {
                _settings.DataOptions += ",decimalSeparator:'" + _settings.NumberFormat.CurrencyDecimalSeparator + "'";
            }
            if (!string.IsNullOrEmpty(_settings.NumberFormat.CurrencySymbol))
            {
                if (_settings.CurrencySymbolPosition == 0)
                {
                    _settings.DataOptions += ",prefix:'" + _settings.NumberFormat.CurrencySymbol + "'";
                }
                _settings.DataOptions += ",suffix:'" + _settings.NumberFormat.CurrencySymbol + "'";
            }

            var numberBox = GetTopTag("input", "easyui-numberbox", _settings);
            numberBox.MergeAttribute("value", _settings.Value.ToString(CultureInfo.InvariantCulture));

            return MvcHtmlString.Create(numberBox.ToString());
        }
    }

    public class NumberBoxSettings : SettingsBase
    {
        public string Style { get; set; }
        public decimal Value { get; set; }

        /// <summary>
        /// Gets or sets the number format.
        /// + NumberFormat.CurrencyDecimalDigits
        /// + NumberFormat.CurrencyGroupSeparator
        /// + CurrencyDecimalSeparator
        /// + NumberFormat.CurrencySymbol
        /// </summary>
        /// <value>
        /// The number format.
        /// </value>
        public NumberFormatInfo NumberFormat { get; set; }
        public int CurrencySymbolPosition { get; set; }
    }
}
